package cn.itsource.controller;

import cn.itsource.domain.Provider;
import cn.itsource.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<Provider> providerList = providerService.findAll();
        model.addAttribute("providers", providerList);
        return "provider/providerList";
    }

    @RequestMapping("/delete")
    public String del(Integer id) {
        providerService.delete(id);
        return "redirect:/provider/list";
    }

    @RequestMapping("/jump")
    public String jump(Integer id, Model model) {
        if (id != null) {
            Provider providerServiceOne = providerService.findOne(id);
            model.addAttribute("provider", providerServiceOne);
            return "provider/providerUpdate";
        }
        return "provider/providerAdd";
    }

    @RequestMapping("/add")
    public String add(Provider provider) {
        providerService.add(provider);
        return "redirect:/provider/list";
    }

    @RequestMapping("/update")
    public String update(Provider provider) {
        providerService.update(provider);
        return "redirect:/provider/list";
    }


    @RequestMapping("/view")
    public String view(Integer id, Model model) {
        Provider providerServiceOne = providerService.findOne(id);
        model.addAttribute("provider", providerServiceOne);
        return "provider/providerView";
    }
}
